package com.zhiziai.aiproxy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.zhiziai.aiproxy.config.SerializerConfig;

import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author: liuxinyu06
 * @description:
 * @create: 2021-09-01
 **/
public class JsonUtil {
    public static SerializeConfig mapping = new SerializeConfig();

    public JsonUtil() {
    }

    public static String toJson(Object obj) {
        return JSON.toJSONString(obj, mapping, new SerializerFeature[0]);
    }

    public static <T> T fromJson(String json, Class<T> classOfT) {
        return JSON.parseObject(json, classOfT);
    }

    public static <T> T fromJson(String json) {
        return JSON.parseObject(json, new TypeReference<T>() {
        }, new Feature[0]);
    }

    public static <T> T fromJson(String json, Type typeOfT) {
        return JSON.parseObject(json, typeOfT, new Feature[0]);
    }

    static {
        mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        mapping.put(LocalDateTime.class, new SerializerConfig());
    }
}
